package com.zeratul.cloud.demo1.controller;

import com.zeratul.cloud.demo1.service.IUserService;
import com.zeratul.common.core.result.R;
import com.zeratul.common.satoken.domain.entity.SysUser;
import com.zeratul.common.satoken.utils.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {

    private static AtomicInteger count = new AtomicInteger(0);

    /**
     * 压力测试的请求
     * @return
     */
    @GetMapping
    public String test() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("接收到请求！" + count.incrementAndGet());
        return "测试成功";
    }

    /**
     * 测试登录
     */
    @GetMapping("/login")
    public R login() {
        LoginHelper.login(new SysUser().setSysUserId(10086L));
        return R.success(LoginHelper.getToken());
    }

    @Autowired
    private IUserService userService;

    /**
     * 策略模式测试
     */
    @GetMapping("/strategy")
    public R strategy(Integer id) {
        userService.add(id);
        return R.success();
    }
}
